// 多态案例 - 计算器类

#include <iostream>
#include <string>
using namespace std;

// 普通实现
class Calculactor
{
public:
    int get_result(string oper)
    {
        if (oper == "+")
            return m_num1 + m_num2;
        else if (oper == "-")
            return m_num1 - m_num2;
        else if (oper == "*")
            return m_num1 * m_num2;
        // 如果需要提供新的运算，需要修改源码
    }

public:
    int m_num1;
    int m_num2;
};

void test01()
{
    // 普通实现测试
    Calculactor c;
    c.m_num1 = 10;
    c.m_num2 = 5;
    cout << c.m_num1 << " + " << c.m_num2 << " = " << c.get_result("+") << endl;

    cout << c.m_num1 << " - " << c.m_num2 << " = " << c.get_result("-") << endl;

    cout << c.m_num1 << " * " << c.m_num2 << " = " << c.get_result("*") << endl;
}

// 多态实现
// 多态优点：代码组织结构清晰，可读性强，利于前期和后期的扩展以及维护

// 抽象计算器类
class AbstractCalculator
{
public:
    int m_num1;
    int m_num2;
    virtual int get_result()
    {
        return 0;
    }
};

// 加法计算器类
class AddCalculator : public AbstractCalculator
{
public:
    int get_result()
    {
        return m_num1 + m_num2;
    }
};

// 减法计算器类
class SubCalculator : public AbstractCalculator
{
public:
    int get_result()
    {
        return m_num1 - m_num2;
    }
};

// 乘法计算器类
class MulCalculator : public AbstractCalculator
{
public:
    int get_result()
    {
        return m_num1 * m_num2;
    }
};

void test02()
{
    // 创建加法计算器
    AbstractCalculator *abc = new AddCalculator;
    abc->m_num1 = 10;
    abc->m_num2 = 5;
    cout << abc->m_num1 << " + " << abc->m_num2 << " = " << abc->get_result() << endl;
    delete abc;  // 用完了记得要销毁

    // 创建减法计算器
    abc = new SubCalculator;
    abc->m_num1 = 10;
    abc->m_num2 = 5;
    cout << abc->m_num1 << " - " << abc->m_num2 << " = " << abc->get_result() << endl;
    delete abc;

    // 创建乘法计算器
    abc = new SubCalculator;
    abc->m_num1 = 10;
    abc->m_num2 = 5;
    cout << abc->m_num1 << " * " << abc->m_num2 << " = " << abc->get_result() << endl;
    delete abc;
}

int main()
{
    // 普通写法测试
    test01();

    // 多态写法测试
    test02();

    return 0;
}